/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChildManager.java
 *
 * Created on Aug 11, 2010, 4:51:10 AM
 */

package GUI;
import config.Database_function;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author QUOCTHGC60050
 */
public class ChildManager extends javax.swing.JPanel {
    Connection cn= Database_function.setConnection();
    Statement st;
    ResultSet re;
    Vector table = new Vector();
    Vector label= new Vector();

    public ChildManager() {


        initComponents();
        this.setSize(830, 440);
        
        initTableC();

    }

    public void addTableC()
    {
        try
        {
            Vector temp= new Vector();

            temp.add(re.getString("ChildID"));
            temp.add(re.getString("FirstName"));
            temp.add(re.getString("LastName"));
            temp.add(re.getString("AgeGroupID"));
            temp.add(re.getString("Gender"));
            temp.add(re.getString("Address"));
            temp.add(re.getString("Status"));

            table.add(temp);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void initTableC()
    {
        try
        {
            st= cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re=st.executeQuery("select * from tbl_Child");

            label.add("ID");label.add("FirstName");label.add("LastName");label.add("AgeGroup");label.add("Gender");label.add("Address");label.add("Status");

           while(re.next())
            {
                addTableC();
            }

            this.jTableChild.setModel(new DefaultTableModel (table,label));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void SearchC()
    {

        String searchString= this.jTextFieldSearch.getText();
        String searchType= (String) this.jComboBoxSearch.getSelectedItem();

        try
        {
            st= cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

            table.removeAllElements();

            if (searchType.equals("Name"))
            {
                re=st.executeQuery("select * from tbl_Child where (LastName like '%"+searchString+"%') or (FirstName like '%"+searchString+"%')");
                while(re.next())
                {
                    addTableC();
                }
            }

            else
            {
                re=st.executeQuery("select * from tbl_Child where "+searchType+" like '%"+searchString+"%'");
                while(re.next())
                {
                    addTableC();
                }
            }

            this.jTableChild.setModel(new DefaultTableModel (table,label));

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableChild = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                return false;
            }
        };
        btnEdit = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jComboBoxSearch = new javax.swing.JComboBox();
        jTextFieldSearch = new javax.swing.JTextField();
        jCheckBoxAdvancedSearch = new javax.swing.JCheckBox();
        jPanelAdvancedSearch = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtSearchAddress = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtToDate = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtFromDate = new com.toedter.calendar.JDateChooser();

        setBackground(new java.awt.Color(227, 239, 255));
        setPreferredSize(new java.awt.Dimension(800, 400));

        jLabel1.setText("Search: ");

        jLabel2.setText("Base on:");

        jTableChild.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "First Name", "Last Name", "AgeGroup", "Gender", "Address", "Status"
            }
        ));
        jTableChild.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableChildMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableChildMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableChild);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit_1.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Search1.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jComboBoxSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Name", "ChildID", "AgeGroupID", "Gender", "Status", " " }));

        jCheckBoxAdvancedSearch.setBackground(new java.awt.Color(227, 239, 255));
        jCheckBoxAdvancedSearch.setText("     Advanced Search");
        jCheckBoxAdvancedSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxAdvancedSearchActionPerformed(evt);
            }
        });

        jPanelAdvancedSearch.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelAdvancedSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelAdvancedSearch.setEnabled(false);
        jPanelAdvancedSearch.setFocusable(false);
        jPanelAdvancedSearch.setOpaque(false);
        jPanelAdvancedSearch.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("Address:");
        jPanelAdvancedSearch.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));
        jPanelAdvancedSearch.add(txtSearchAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 10, 190, -1));

        jLabel4.setText("Registration Date: ");
        jPanelAdvancedSearch.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        txtToDate.setDateFormatString("MMMM/dd/yyyy");
        jPanelAdvancedSearch.add(txtToDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 40, 190, -1));

        jLabel5.setText("From");
        jPanelAdvancedSearch.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, -1, -1));

        jLabel6.setText("to");
        jPanelAdvancedSearch.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 40, -1, -1));

        txtFromDate.setDateFormatString("MMMM/dd/yyyy");
        jPanelAdvancedSearch.add(txtFromDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 40, 190, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBoxAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(649, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(268, 268, 268)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(69, 69, 69)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(274, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel2)
                        .addGap(60, 60, 60)
                        .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                        .addComponent(btnSearch)
                        .addGap(23, 23, 23))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addComponent(jCheckBoxAdvancedSearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnEdit))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        int d=this.jTableChild.getSelectedRow();// get selected row on table
        if(d>=0)
        {
            String idChild=(String)this.jTableChild.getValueAt(d, 0);
            EditChild ed = new EditChild(idChild);
            ed.setVisible(true);
        }
}//GEN-LAST:event_btnEditActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        SearchC();
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        AddNewChild ac = new AddNewChild();
        ac.setVisible(true);

    }//GEN-LAST:event_btnAddActionPerformed

    private void jTableChildMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableChildMouseClicked
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTableChildMouseClicked

    private void jTableChildMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableChildMouseReleased
        // TODO add your handling code here:
        int selectedRow =  jTableChild.getSelectedRow();
        if (evt.getClickCount()==2)
        {
            Integer ChildID = Integer.parseInt((String)jTableChild.getValueAt(selectedRow, 0));
            detailChild dc= new detailChild(null,true,ChildID);
            dc.setVisible(true);
        }
    }//GEN-LAST:event_jTableChildMouseReleased

    private void jCheckBoxAdvancedSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxAdvancedSearchActionPerformed
        // TODO add your handling code here:
        boolean check = this.jCheckBoxAdvancedSearch.isSelected();
        if (check==true) {
            this.jPanelAdvancedSearch.setVisible(true);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
        } else {
            this.jPanelAdvancedSearch.setVisible(false);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
            this.jComboBoxSearch.addItem("NannyID");
        }
}//GEN-LAST:event_jCheckBoxAdvancedSearchActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSearch;
    private javax.swing.JCheckBox jCheckBoxAdvancedSearch;
    private javax.swing.JComboBox jComboBoxSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanelAdvancedSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableChild;
    private javax.swing.JTextField jTextFieldSearch;
    private com.toedter.calendar.JDateChooser txtFromDate;
    private javax.swing.JTextField txtSearchAddress;
    private com.toedter.calendar.JDateChooser txtToDate;
    // End of variables declaration//GEN-END:variables

}
